package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorTrademark;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ProvideCommodityServiceRange;
import guoanmaker.operator.business.modelnobind.OperatorTrademarkVo;
import guoanmaker.operator.business.modelnobind.ProvideCommodityMove;
import guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove;
import guoanmaker.operator.business.repository.OperatorTrademarkRepository;
import guoanmaker.operator.business.repository.ProvideCommodityRepository;
import guoanmaker.operator.business.repository.ProvideCommodityServiceRangeRepository;

@Service
public class OperatorTrademarkService implements OperatorTrademarkServiceInterface {
	@Autowired
private OperatorTrademarkRepository operatorTrademarkRepository;
	@Autowired
	private ProvideCommodityRepository provideCommodityRepository;
	 @PersistenceContext
		private EntityManager manager;
	 @Autowired
	 private ProvideCommodityServiceRangeRepository provideCommodityServiceRangeRepository;
	@Override
	@Transactional
	public OperatorTrademark saveOperatorTrademark(OperatorTrademark operatorTrademark) {
		// TODO Auto-generated method stub
		return operatorTrademarkRepository.save(operatorTrademark);
	}
	
	 @Transactional
	  public void delectAllBycitycode(String citycode, int type){
		  String jpql="delete  from OperatorTrademark  u where u.citycode=:citycode and type=:type";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("citycode", citycode);
			query2.setParameter("type", type);
			query2.executeUpdate();
		  
	  }

	@Override
	public void deleteOperatorTrademark(String id) {
		// TODO Auto-generated method stub
		operatorTrademarkRepository.delete(id);
	}

	@Override
	public List<OperatorTrademark> selectOperatorTrademark(String citycode,int type) {
		// TODO Auto-generated method stub
		return operatorTrademarkRepository.selectByOperatorTrademark(type, citycode);
	}

	@Override
	public List<OperatorTrademarkVo> selectOperatorTrademarkInformation(String citycode, int type) {
		// TODO Auto-generated method stub
		List<OperatorTrademark> trademarks=operatorTrademarkRepository.selectByOperatorTrademark(type, citycode);
		List<OperatorTrademarkVo>  tradevos=new ArrayList<OperatorTrademarkVo>();
		for(OperatorTrademark trademark:trademarks){
			if(trademark.getCommodityNum()!=null && !"".equals(trademark.getCommodityNum())){
				ProvideCommodity comdity=provideCommodityRepository.selectByCommodityBypraThree(Integer.parseInt(trademark.getCommodityNum()));
				if(comdity!=null){
					OperatorTrademarkVo tradevo=new OperatorTrademarkVo();
					if(comdity.getIsTerritoryRestriction().equals("1")){
						List<ProvideCommodityServiceRange> comrang=provideCommodityServiceRangeRepository.selectBycitycodeandid(citycode,comdity.getId());
						if(comrang.size()>0){
							tradevo.setFinalPrice(comrang.get(0).getFinalPrice()+"");
							tradevo.setCommidityId(comdity.getId());
							tradevo.setCommodityName(comdity.getCommodityName());
							tradevo.setCommodityNum(comdity.getCommodityCode()+"");
							/*if (type == 1) {
								tradevo.setSmall_pic(trademark.getComPrcUrl());
							} else {
								tradevo.setSmall_pic(comdity.getCommodity_smallimage());
							}*/
							tradevo.setSmall_pic(comdity.getCommodity_smallimage());
							tradevo.setServiceType(comdity.getType());
							tradevos.add(tradevo);
						}
						
						
					}else if(comdity.getIsTerritoryRestriction().equals("2")){
						tradevo.setFinalPrice(comdity.getFinalPrice()+"");
						tradevo.setCommidityId(comdity.getId());
						tradevo.setCommodityName(comdity.getCommodityName());
						tradevo.setCommodityNum(comdity.getCommodityCode()+"");
						/*if (type == 1) {
							tradevo.setSmall_pic(trademark.getComPrcUrl());
						} else {
							tradevo.setSmall_pic(comdity.getCommodity_smallimage());
						}*/
						tradevo.setSmall_pic(comdity.getCommodity_smallimage());
						tradevo.setServiceType(comdity.getType());
						tradevos.add(tradevo);
					}
					
					
				}
			}	
		}
		
		
		return tradevos;
	}
	
	/**
	   * 
	      * @Title: selectOperatorTrademarkByCommodityNum  
	      * @Description: 根据商品编号 城市编码 查询商品详情  
	      * @param @param commodityNum
	      * @param @param cityCode
	      * @return AjaxJson      
	      * @throws
	   */
	@Override
	public OperatorTrademarkVo selectOperatorTrademarkByCommodityNum(Integer commodityNum, String cityCode) {
		OperatorTrademarkVo trademarkVo = null;
		ProvideCommodity commodity = provideCommodityRepository.selectByCommodityBypraThree(commodityNum);
		if (commodity != null) {
			
			if (commodity.getIsTerritoryRestriction().equals("1")) {
				List<ProvideCommodityServiceRange> commodityServiceRanges = commodity.getServiceRange();
				if (commodityServiceRanges != null && commodityServiceRanges.size() >0) {
					for (ProvideCommodityServiceRange provideCommodityServiceRange : commodityServiceRanges) {
						if (cityCode.equals(provideCommodityServiceRange.getCity().getCode())) {
							trademarkVo = new OperatorTrademarkVo();
							trademarkVo.setCommidityId(commodity.getId());
							trademarkVo.setCommodityName(commodity.getCommodityName());
							trademarkVo.setSmall_pic(commodity.getCommodity_smallimage());
							trademarkVo.setCommodityNum(String.valueOf(commodity.getCommodityCode()));
							trademarkVo.setFinalPrice(String.valueOf(provideCommodityServiceRange.getFinalPrice()));
							break;
							
						}
					}
				}
				
			} else {
				trademarkVo = new OperatorTrademarkVo();
				trademarkVo.setCommidityId(commodity.getId());
				trademarkVo.setCommodityName(commodity.getCommodityName());
				trademarkVo.setSmall_pic(commodity.getCommodity_smallimage());
				trademarkVo.setCommodityNum(String.valueOf(commodity.getCommodityCode()));
				trademarkVo.setFinalPrice(String.valueOf(commodity.getFinalPrice()));
			}
		}
		return trademarkVo;
	}

}
